package com.innovations.inn.service.impl;

import com.innovations.common.utils.uuid.IdGeneratorSnowflake;
import com.innovations.inn.domain.ResearchInstitution;
import com.innovations.inn.domain.ResearchInstitutionModify;
import com.innovations.inn.domain.response.ResponseItemCount;
import com.innovations.inn.mapper.ResearchInstitutionModifyMapper;
import com.innovations.inn.service.IResearchInstitutionModifyService;
import com.innovations.inn.service.IResearchInstitutionService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import static com.innovations.common.utils.MyBeanUtils.copyProperties;

/**
 * 科研机构修改记录Service业务层处理
 *
 * @author dp
 * @date 2024-11-09
 */
@Service
public class ResearchInstitutionModifyServiceImpl implements IResearchInstitutionModifyService {
    @Autowired
    ResearchInstitutionModifyMapper researchInstitutionModifyMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;
    @Autowired
    private IResearchInstitutionService researchInstitutionService;

    /**
     * 查询科研机构修改记录
     *
     * @param modifyId 科研机构修改记录主键
     * @return 科研机构修改记录
     */
    @Override
    public ResearchInstitutionModify selectResearchInstitutionModifyByModifyId(Long modifyId) {
        return researchInstitutionModifyMapper.selectResearchInstitutionModifyByModifyId(modifyId);
    }

    /**
     * 查询科研机构修改记录列表
     *
     * @param researchInstitutionModify 科研机构修改记录
     * @return 科研机构修改记录
     */
    @Override
    public List<ResearchInstitutionModify> selectResearchInstitutionModifyList(ResearchInstitutionModify researchInstitutionModify) {
        return researchInstitutionModifyMapper.selectResearchInstitutionModifyList(researchInstitutionModify);
    }

    /**
     * 查询科研机构修改记录合计
     *
     * @param researchInstitutionModify 科研机构修改记录
     * @return 科研机构修改记录合计
     */
    @Override
    public List<ResponseItemCount> selectResearchInstitutionModifyCount(ResearchInstitutionModify researchInstitutionModify) {
        return researchInstitutionModifyMapper.selectResearchInstitutionModifyCount(researchInstitutionModify);
    }

    /**
     * 新增科研机构修改记录
     *
     * @param researchInstitutionModify 科研机构修改记录
     * @return 结果
     */
    @Override
    public int insertResearchInstitutionModify(ResearchInstitutionModify researchInstitutionModify) {
        return researchInstitutionModifyMapper.insertResearchInstitutionModify(researchInstitutionModify);
    }

    /**
     * 修改科研机构修改记录
     *
     * @param researchInstitutionModify 科研机构修改记录
     * @return 结果
     */
    @Override
    public int updateResearchInstitutionModify(ResearchInstitutionModify researchInstitutionModify) {
        // 状态为审核通过时，添加科研机构信息
        if (researchInstitutionModify.getState() == 2) {
            // 获取科研机构信息
            ResearchInstitution researchInstitution = researchInstitutionService
                    .selectResearchInstitutionByInstitutionId(researchInstitutionModify.getInstitutionId());
            ResearchInstitution researchInstitutionNew = copyProperties(researchInstitutionModify, ResearchInstitution.class);
            // 新增或修改科研机构信息
            if (researchInstitution == null || researchInstitution.getInstitutionId() == null) {
                researchInstitutionService.insertResearchInstitution(researchInstitutionNew);
            } else {
                researchInstitutionService.updateResearchInstitution(researchInstitutionNew);
            }
        }
        // 更新科研机构修改记录
        return researchInstitutionModifyMapper.updateResearchInstitutionModify(researchInstitutionModify);
    }

    /**
     * 批量删除科研机构修改记录
     *
     * @param modifyIds 需要删除的科研机构修改记录主键
     * @return 结果
     */
    @Override
    public int deleteResearchInstitutionModifyByModifyIds(Long[] modifyIds) {
        return researchInstitutionModifyMapper.deleteResearchInstitutionModifyByModifyIds(modifyIds);
    }

    /**
     * 删除科研机构修改记录信息
     *
     * @param modifyId 科研机构修改记录主键
     * @return 结果
     */
    @Override
    public int deleteResearchInstitutionModifyByModifyId(Long modifyId) {
        return researchInstitutionModifyMapper.deleteResearchInstitutionModifyByModifyId(modifyId);
    }

    /**
     * 实体数据补全
     *
     * @param researchInstitutionModify 实体
     * @param userId                    用户id
     * @param crateType                 创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(ResearchInstitutionModify researchInstitutionModify, Long userId, Integer crateType) {
        // 修改记录标识
        if (researchInstitutionModify.getModifyId() == null) {
            researchInstitutionModify.setModifyId(idGeneratorSnowflake.nextId());
        }

        // 科研机构标识
        if (researchInstitutionModify.getInstitutionId() == null) {
            // 根据科研机构名称获取科研机构标识
            ResearchInstitution researchInstitution = new ResearchInstitution();
            researchInstitution.setInstitutionName(researchInstitutionModify.getInstitutionName());
            List<ResearchInstitution> researchInstitutionList = researchInstitutionService.selectResearchInstitutionList(researchInstitution);
            // 判断科研机构列表为空，则创建新科研机构标识
            if (researchInstitutionList == null || researchInstitutionList.isEmpty()) {
                researchInstitutionModify.setInstitutionId(idGeneratorSnowflake.nextId());
            } else {
                researchInstitutionModify.setInstitutionId(researchInstitutionList.get(0).getInstitutionId());
            }
        }

        // 创建模式
        if (crateType == 1) {
            // 创建人、创建时间
        }
        // 修改模式
        else if (crateType == 2) {

        }

        // 逻辑删除
        if (researchInstitutionModify.getIsDeleted() == null) {
            researchInstitutionModify.setIsDeleted(0L);
        }
    }

}
